package net.example.activity.adapter;


import android.content.Context;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import net.example.manager.ClientGlobalCache;
import net.example.model.FriendCatalogContainer;
import net.example.model.GGUser;
import net.example.model.UserStatus;
import net.example.ggtalk.R;
import net.example.utils.HeadImgHelper;
import net.example.utils.android.AndroidUI;

import java.util.ArrayList;

/**
 * Created by ZN on 2016/1/14.
 */
public class FriendAdapter  extends BaseExpandableListAdapter {
    private Context context;
    private LayoutInflater inflater;
    private ArrayList<FriendCatalogContainer> friendCatalogList;

    public FriendAdapter(Context context,ArrayList<FriendCatalogContainer> catalogList) {
        this.context = context;
        inflater = LayoutInflater.from(context);
        this.friendCatalogList =catalogList;
    }

    // 返回父列表个数
    @Override
    public int getGroupCount() {
        return friendCatalogList.size();
    }

    // 返回子列表个数
    @Override
    public int getChildrenCount(int groupPosition) {
        return friendCatalogList.get(groupPosition).getChildSize();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return friendCatalogList.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return friendCatalogList.get(groupPosition).getChild(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {

        return true;
    }

    @Override
    public View getGroupView(int catalogPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        FriendCatalogHolder friendCatalogHolder = null;
        if (convertView == null || convertView.getTag() == null) {
            friendCatalogHolder = new FriendCatalogHolder();
            convertView = inflater.inflate(R.layout.friend_group_item, null);
            friendCatalogHolder.name = (TextView) convertView
                    .findViewById(R.id.groupName);
            friendCatalogHolder.childCount = (TextView) convertView
                    .findViewById(R.id.count);
            friendCatalogHolder.imageView = (ImageView) convertView
                    .findViewById(R.id.expand);
            convertView.setTag(friendCatalogHolder);
        } else {
            friendCatalogHolder = (FriendCatalogHolder) convertView.getTag();
        }
        FriendCatalogContainer container = this.friendCatalogList.get(catalogPosition);
        friendCatalogHolder.name.setText(container.getgroupName());
        friendCatalogHolder.childCount.setText("( "+ container.getChildSize() +" )");

        if (isExpanded)// ture is Expanded or false is not isExpanded
        {
//            friendCatalogHolder.imageView.setRotation(45);
           friendCatalogHolder.imageView.setImageResource(R.drawable.shrank);
        } else {
            friendCatalogHolder.imageView.setImageResource(R.drawable.expand);
//            friendCatalogHolder.imageView.setRotation(0);

        }
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        FriendHolder friendHolder = null;
        if (convertView == null) {
            friendHolder = new FriendHolder();
            convertView = inflater.inflate(R.layout.friend_child, null);

            friendHolder.imageView = (ImageView) convertView
                    .findViewById(R.id.ct_photo);
            friendHolder.statusView = (ImageView) convertView
                    .findViewById(R.id.ct_status);
            friendHolder.name = (TextView) convertView
                    .findViewById(R.id.ct_name);
            friendHolder.sign = (TextView) convertView
                    .findViewById(R.id.ct_sign);
            friendHolder.layout = (View) convertView
                    .findViewById(R.id.layout);
            friendHolder.layout.setId(View.generateViewId());
            convertView.setTag(friendHolder);
        } else {
            friendHolder = (FriendHolder) convertView.getTag();
        }
        GGUser user = friendCatalogList.get(groupPosition).getChild(childPosition);
        if(user.getUserID().equals(ClientGlobalCache.getInstance().getCurrentUser().getUserID()))
        {
            friendHolder.imageView.setImageResource(R.drawable.file_transfer);
            friendHolder.name.setText("文件传输助手");
            friendHolder.sign.setText("");
            friendHolder.statusView.setVisibility(View.GONE);
        }else {
            HeadImgHelper.setUserHeadImg(friendHolder.imageView,user);
            UserStatus status = user.getUserStatus();
            friendHolder.statusView.setImageResource(UserStatus.getUserStatusResource(status, false));
            UserStatus.setImageBackgroundGrayByStatus(status, friendHolder.imageView, friendHolder.statusView);
            if (user.statusChanged) {
                AndroidUI.animationAlpha(friendHolder.imageView);
                user.statusChanged = false;
            }
            friendHolder.name.setText(user.getCommentName());
            friendHolder.sign.setText(user.getSignature());
        }


//        else if (user.isNewMsg) {
//            AndroidUI.shakeAnimationVertical(Animation.INFINITE, friendHolder.layout);去掉新消息抖动
//        } else {
//            AndroidUI.clearAnimation(friendHolder.layout);
//        }

        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    //好友分组目录
    class FriendCatalogHolder {
        TextView name, childCount;
        ImageView imageView;
    }

    class FriendHolder {
        View layout;
        TextView name,sign;
        ImageView imageView,statusView;
    }
}
